import os, sys, getopt, string

from subprocess import *
from server import server


def help_message():
    print '''\n\nSonar - A Media Transcoding and Streaming Server.
    Options: -h, --help            -- displays this help message.
             -v, --version         -- displays Sonar's version.'''
             #Not supported, yet... -p PATH, --path=PATH  -- use this for passing the path of the shared files folder (default is .\\media\\). See documentation for specific details.
    sys.exit(0)
#help_message()


def version_message():
    print '''\n\nSonar - A Media Transcoding and Streaming Server.
Server-only version 0.8.
For more info, gui-version, and source code, please refer to Sonar website at Google Code.'''
    sys.exit(0)
#version_message()


if __name__ == '__main__':
    path = os.getcwd()
    sys.path.append(path)

    try:
        options, xarguments = getopt.getopt(sys.argv[1:],'hv:', ['version', 'help'])
        #options, xarguments = getopt.getopt(sys.argv[1:],'hvp:', ['path=', 'version', 'help'])
    except getopt.error:
        print 'Error: You tried to use an unknown option or the argument for an option that requires it was missing. Try `sonar.py -h\' for more information.'
        sys.exit(0)

    for a in options[:]:
        if a[0] == '-h' or a[0] == '--help':
            help_message()

    for a in options[:]:
        if a[0] == '-v' or a[0] == '--version':
            version_message()

#    for a in options[:]:
#        if (a[0] == '-p' or a[0] == '--path') and a[1] != '':
#            path = a[1]
#            options.remove(a)
#            break
#        elif a[0] == '--path' and a[1] == '':
#            print '--path expects an argument. See help for more info.'
#            sys.exit(0)

    server.serve_forever()
#main


    ##os.system("start gui\\GUISonar.py")
    #server.serve_forever()

    #serverProcess = Popen("python server\\server.py")
    #guiP = call("python gui\\GUISonar.py")
    #os.system("taskkill /F /PID %d" % serverProcess.pid)

    ##status = os.waitpid(guiP.pid, 0)
    #serverRunning.server_close()

